//
//  IntegralViewController.m
//  cm
//
//  Created by N-X on 2018/2/2.
//  Copyright © 2018年 feihu. All rights reserved.
//  积分商城

#import "IntegralViewController.h"
#import "CollectionViewCell.h"
#import "IntegralGoodsViewController.h"

#define tabBarHeight  self.tabBarController.tabBar.frame.size.height

@interface IntegralViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@end

@implementation IntegralViewController
{
    NSInteger page;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self reloadData];
    
    [self createShoppingMallView];
}

-(void)reloadData
{
    
    [[ToolNet sharedManager] getCarsList:nil pageNum:1 pageSize:16 type:@"2" url:MCarsList completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            [self.dataSource removeAllObjects];
            [self.dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
            [_collectionView reloadData];
            page = 2;
        }
        else {
            [self showMessage:responseJson[@"msg"]];
        }
        [_collectionView.mj_header endRefreshing];
        [_collectionView.mj_footer endRefreshing];
    }];
}

-(void)loadMoreData
{
   
    [[ToolNet sharedManager] getCarsList:nil pageNum:page pageSize:16 type:@"2" url:MCarsList completionHandler:^(NSURLSessionDataTask *task, NSDictionary *responseJson, NSError *error) {
        if ([responseJson[@"code"] isEqual:@1]) {
            if ([responseJson[@"data"][@"pages"] integerValue] >= page) {
                [self.dataSource addObjectsFromArray:responseJson[@"data"][@"list"]];
                [_collectionView.mj_footer endRefreshing];
                page++;
            }
            else{
                [_collectionView.mj_footer endRefreshingWithNoMoreData];
            }
            [_collectionView reloadData];
        }
        else {
            [self showMessage:responseJson[@"msg"]];
            [_collectionView.mj_footer endRefreshing];
        }
    }];
}

-(void)createShoppingMallView
{
    self.titleLabel.text = @"积分商城";
    self.backButton.hidden = NO;
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0.f;
    layout.minimumLineSpacing = 0.5f;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.97 alpha:1];
    [self.view addSubview:_collectionView];
    _collectionView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.baseBar, 0)
    .widthIs(kScreenWidth)
    .heightIs(kScreenHeight - self.baseBarHeight);
    
    [_collectionView registerClass:[CollectionViewCell class] forCellWithReuseIdentifier:@"cellIdentify"];
    
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    _collectionView.mj_header.automaticallyChangeAlpha = YES;
    _collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
    }];
    
    self.dataSource = [NSMutableArray array];
}

#pragma mark -- UICollectionViewDataSource
//定义展示的UICollectionViewCell的个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

//定义展示的Section的个数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellIdentifier = @"cellIdentify";
    CollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    CollectionModel * model = [[CollectionModel alloc] initWithDictionary:self.dataSource[indexPath.row]];
    model.type = 2;
    cell.model = model;
    
    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout
//定义每个UICollectionView的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake((kScreenWidth - 15)/2, (kScreenWidth - 15)/2+70);
}

//定义每个UICollectionView的margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}

#pragma mark --UICollectionViewDelegate
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IntegralGoodsViewController *goodsVC = [[IntegralGoodsViewController alloc] init];
    goodsVC.dataSource = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:goodsVC animated:YES];
}

//返回这个UICollectionView是否可以被选择
-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


@end
